﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		TrackBackNotificationService.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion
 
using System.Collections.Generic;
using System.Net.Mail;
using System.Web.Security;
using Dexter.Core.EventArgs;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Services;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	Implementation of <see cref = "ITrackBackNotificationService" />.
	/// </summary>
	public class TrackBackNotificationService : ITrackBackNotificationService {
		readonly IConfigurationService configurationService;
		readonly IEmailService emailService;
		readonly IEmailTemplateService emailTemplateService;
		readonly ILogger logger;
		readonly IUrlBuilder urlBuilder;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "TrackBackNotificationService" /> class.
		/// </summary>
		/// <param name = "trackBackService">The track back service.</param>
		/// <param name = "emailService">The email service.</param>
		/// <param name = "configurationService">The configuration service.</param>
		/// <param name = "emailTemplateService">The email template service.</param>
		/// <param name = "logger">The logger.</param>
		/// <param name = "urlBuilder">The URL builder.</param>
		public TrackBackNotificationService ( ITrackBackService trackBackService , IEmailService emailService , IConfigurationService configurationService , IEmailTemplateService emailTemplateService , ILogger logger , IUrlBuilder urlBuilder ) {
			this.emailService = emailService;
			this.configurationService = configurationService;
			this.emailTemplateService = emailTemplateService;
			this.logger = logger;
			this.urlBuilder = urlBuilder;

			trackBackService.TrackBackAdded += TrackBackServiceTrackBackAdded;
		}

		void TrackBackServiceTrackBackAdded ( object sender , GenericEventArgs <TrackBack> e ) {
			var configuration = configurationService.Configuration;

			if ( !configuration.NotificationConfiguration.NotifyNewComment ) {
				logger.Debug ( "The comment notification service is disabled." );
				return;
			}

			const string TEMPLATE_NAME = "dexter.trackback.newtrackback";

			var template = emailTemplateService.GetTemplate ( TEMPLATE_NAME );

			if ( template == null ) {
				logger.WarnFormat ( "Template '{0}' not found." , TEMPLATE_NAME );
				return;
			}

			IDictionary <string , string> values = new Dictionary <string , string> {
				{"ReferreUrl" , e.Objects.Url.ToString ( )} ,
				{"PostTitle" , e.Objects.Item.Title} ,
				{"ReferrerTitle" , e.Objects.Title} ,
				{"PostUrl" , urlBuilder.Post.Detail ( e.Objects.Item )} ,
			};

			string subject = template.GetFormatedSubject ( values );
			string body = template.GetFormatedBody ( values );

			var currentUser = Membership.GetUser ( e.Objects.Item.Username );

			if ( currentUser == null ) {
				logger.Debug ( "There is a new comment for a post without user. Maybe the user is deleted" );
				currentUser = Membership.GetUser ( configuration.PrincipalAuthor );
			}

			var email = new MailAddress ( currentUser.Email , currentUser.UserName );

			var resultEmail = EmailMessage.CreateNewEmailMessage ( configuration.NotificationConfiguration.SenderEmail , email , subject , body );

			emailService.Queue ( resultEmail );
		}
	}
}
